
var localPlayerID = Players.GetLocalPlayer();
var m_PersonalCardPanels = {};
var plantConfig = {};
var selectedCardPanels ;
var refreshTime = 0.2
// 冷却时间倒计时
function CoolDownTime(plantPanel){
		var cooldownRemaining = plantPanel.Data().residueCooldown
		if (cooldownRemaining<=0){
			plantPanel.FindChildTraverse("Cooldown").visible = false;
			return ;
		}
		plantPanel.FindChildTraverse("Cooldown").visible = true;
		
		var cooldownLength = plantConfig[plantPanel.Data().plantIndex].cooldown;
		
		var cooldownPercent = Math.ceil( 100 * cooldownRemaining / cooldownLength );
		// var deg = -360 * cooldownPercent * 0.01;
		
		plantPanel.FindChildTraverse( "CooldownTimer" ).text = Math.ceil( cooldownRemaining );
		plantPanel.FindChildTraverse( "CooldownOverlay" ).style.width = "100%";
		// plantPanel.FindChildTraverse( "CooldownOverlay" ).style.clip = "radial( 50.0% 50.0%, 0.0deg, "+deg+"deg)";
        plantPanel.FindChildTraverse( "CooldownOverlay" ).style.clip = "rect( 0%, 100%, "+cooldownPercent+"%, 0% )";
        
		
        plantPanel.Data().residueCooldown = cooldownRemaining -refreshTime
		$.Schedule(refreshTime, function (){
			CoolDownTime(plantPanel)
		});
}


function CreatePersonalPlantPanels(){
    var playerMaxPlantPanels = 2
	var parentPanel = $("#mainPanel");
	if ( !parentPanel )
		return;
	//初始化信息
	parentPanel.RemoveAndDeleteChildren();
	m_PersonalCardPanels = []


	plantConfig = CustomNetTables.GetTableValue("plant","plantConfig");
	for ( var i = 0; i < playerMaxPlantPanels; ++i ){
		const id = "plant_panel_"+i;
		var plantPanel = $.CreatePanel( "Panel", parentPanel, id );
		plantPanel.BLoadLayoutSnippet("plant_snippet");
		plantPanel.Data().slot = i;
		plantPanel.Data().plantIndex = i+1;
		plantPanel.Data().residueCooldown = 0
		m_PersonalCardPanels.push( plantPanel );
        plantPanel.FindChildTraverse("plantImage").SetImage("file://{images}/custom_game/loading_screen/wandousheshou_normal_attack.jpg");
        //点击事件
        plantPanel.SetPanelEvent("onmouseactivate", function() {
        //  清除所有 并选中需要的
            SelectPlant(id.substr(-1))
    	})
	}
} 

//更新冷却
function updatePlantCooldown(args){
	m_PersonalCardPanels[args.cardID].Data().residueCooldown = args.cooldownTime
	//冷却读条
	CoolDownTime(m_PersonalCardPanels[args.cardID])
}



//选择卡片植物
function SelectPlant(cardIndex){
	if (selectedCardPanels == cardIndex){
		//  使用技能
		let localHero = Players.GetPlayerHeroEntityIndex( localPlayerID)
		GameUI.SelectUnit(localHero,false) 
		//GameUI.SetCameraTargetPosition(Entities.GetAbsOrigin( localHero ) , 0.1 )
		let ability = Entities.GetAbility( localHero, 0 );
		Abilities.ExecuteAbility( ability, localHero, false ); 
		return ;
	} 
	selectedCardPanels = cardIndex
	for(i=0;i<m_PersonalCardPanels.length;++i){
		m_PersonalCardPanels[i].RemoveClass("onSelectedCardPlant")
	}
	m_PersonalCardPanels[cardIndex].AddClass("onSelectedCardPlant")
	GameEvents.SendCustomGameEventToServer("update_selected_plant", {
		playerId : localPlayerID ,
		plantId : m_PersonalCardPanels[cardIndex].Data().plantIndex,
		cardId : cardIndex,
	})
}





(function(){
	GameEvents.Subscribe( "create_personal_plant_panels", CreatePersonalPlantPanels);
	GameEvents.Subscribe( "update_plant_cooldown", updatePlantCooldown);
	Game.PlayerKey1 = function() {SelectPlant(0)};
	Game.PlayerKey2 = function() {SelectPlant(1)};
	Game.PlayerKey3 = function() {SelectPlant(2)};
	Game.PlayerKey4 = function() {SelectPlant(3)};
	Game.PlayerKey5 = function() {SelectPlant(4)};
	Game.PlayerKey6 = function() {SelectPlant(5)};
	Game.PlayerKey7 = function() {SelectPlant(6)};
	Game.PlayerKey8 = function() {SelectPlant(7)};
	Game.PlayerKey9 = function() {SelectPlant(8)};
	Game.PlayerKey0 = function() {SelectPlant(9)};


})();
